import { ERROR_MSG_DURATION, NO_ERROR_MSG_CODE } from '@/constants'

import { useMessage } from 'naive-ui'

const errorMsgStack = new Map([])

function addErrorMsg(error) {
    errorMsgStack.set(error.code, error.msg)
}

function removeErrorMsg(error) {
    errorMsgStack.delete(error.code)
}

function hasErrorMsg(error) {
    return errorMsgStack.has(error.code)
}

export function showErrorMsg(error) {
    if (!error.type) error.type = 'backend'
    if (!error.msg || NO_ERROR_MSG_CODE.includes(error.code) || hasErrorMsg(error)) return


    addErrorMsg(error)
    window.console.warn(error.code, error.msg)
    window.$message.error(error.msg, { duration: ERROR_MSG_DURATION })
    setTimeout(() => {
        removeErrorMsg(error)
    }, ERROR_MSG_DURATION)
}
export function showSuccessMsg(msg) {
    window.$message.success(msg, { duration: ERROR_MSG_DURATION })
   
}